/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */
$().ready(function () {

	$("#new_user_form").validate({

		// debug:true,

		rules:{
			username: {
				required: true,
				rangelength: [6, 18],
				usernameCheck: true,
				remote: "check_username"
			},
			password: {
				required: true,
				rangelength: [6,12]
			},
			cfm_password: {
				equalTo: "#password"
			},
			// gender: "required",
			departmentId: "required",
			designationId: "required",
			permissionGroupId: "required",
			mobile:{
				required: true,
				chineseMobileNumber: true
			},
			email:{
				required: true,
				email:true
			},
			salary: {
				required: true,
				number: true,
				range: [1, 1000000]
			}
		},

		messages:{
			username:{
				usernameCheck: "Username should start with a letter and only contains letters and digits",
				remote: "Username already exists"
			},
			mobile: "Invalid Mobile Number",
			cfm_password: "Password does not match"
		},

		submitHandler: function (form) {
			$("#password").val(getEncodedPassword($("#password").val()));
			$(form).ajaxSubmit({
				url:"new_user",
				type: "post",
				dataType: "json",
				success: function (data) {
					if(data.status=="success"){
						showMessagePopup("Information", "User Successfully Inserted", function () {
							window.top.close();
						});
					} else {
						showMessagePopup("Error", data.errorMessage + ", There could be duplicate mobile no. or email.");
					}
				},

				error: function (jqHXR, textStatus, error) {
					console.log(error);
				}
			});
		}
	});

	// $("#new_user_form").submit(function () {
	// 	return false;
	// })
});
